<?php
 /*
     * 导出分仓数据
     */
    public function ExportPart($json) {
        if ($this->isNotOnline()) {
            return;
        }

        $db=M();
        $sql="SELECT id,lot FROM `t_lot_part` WHERE 1=1 ORDER by lot DESC";
        $t_lot_part=$db->query($sql);
        $t_lot_part_id=$t_lot_part[0]['id'];
        $lot=$t_lot_part[0]['lot'];

        $sql="select * from t_part where lot_id='$t_lot_part_id'";
        $data=$db->query($sql);




        // 导出Excel
        $excel = new \PHPExcel();

        $sheet = $excel->getActiveSheet();
        if (! $sheet) {
            $sheet = $excel->createSheet();
        }

        $sheet->setTitle("分仓数据名细");

        $sheet->getRowDimension('1')->setRowHeight(22);
        $sheet->setCellValue("A1", "分仓数据名细 当前批次为$lot");

        $sheet->getColumnDimension('A')->setWidth(15);
        $sheet->setCellValue("A2", "条码");

        $sheet->getColumnDimension('B')->setWidth(15);
        $sheet->setCellValue("B2", "商品描述");

        $sheet->getColumnDimension('C')->setWidth(15);
        $sheet->setCellValue("C2", "要入仓数量");

        $sheet->getColumnDimension('D')->setWidth(10);
        $sheet->setCellValue("D2", "已入仓数量");

        $sheet->getColumnDimension('E')->setWidth(30);
        $sheet->setCellValue("E2", "未入仓数量");

        $sheet->getColumnDimension('F')->setWidth(30);
        $sheet->setCellValue("F2", "发往仓库");



        foreach ( $data as $i => $v ) {
            $row = $i + 3;
            $sheet->setCellValue("A" . $row, $v["barcode"]);
            $sheet->setCellValue("B" . $row, $v["name"]);
            $insucount=$v["sumcount"]-$v["count"];
            $sheet->setCellValue("C" . $row, $v["sumcount"]);
            $sheet->setCellValue("D" . $row, $insucount);
            $sheet->setCellValue("E" . $row, $v["count"]);
            $sheet->setCellValue("F" . $row, $v["warehouse"]);


        }

        // 画表格边框
        $styleArray = array(
                'borders' => array(
                        'allborders' => array(
                                'style' => 'thin'
                        )
                )
        );
        $lastRow = count($data) + 2;
        $sheet->getStyle('A2:F' . $lastRow)->applyFromArray($styleArray);

        $fileName = "分仓数据名细";

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $fileName . '.xls"');
        header('Cache-Control: max-age=0');

        $writer = \PHPExcel_IOFactory::createWriter($excel, "Excel5");
        $writer->save("php://output");
    }